package cn.com.api.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiU_match;
import cn.com.api.entity.ApiU_match_prize;
import cn.com.api.entity.ApiU_match_works;
import cn.com.api.mapper.ApiU_matchMapper;
import cn.com.api.mapper.ApiU_match_prizeMapper;
import cn.com.api.mapper.ApiU_match_worksMapper;
import cn.com.api.service.ApiU_match_worksService;
import cn.com.api.service.impl.base.U_match_worksServiceImplBase;
import cn.com.util.Utils;

/**
 * 
 **/
@Service
public class ApiU_match_worksServiceImpl extends U_match_worksServiceImplBase implements ApiU_match_worksService {

	@Autowired
	private ApiU_match_worksMapper apiU_match_worksMapper;
	@Autowired
	private ApiU_match_prizeMapper apiU_match_prizeMapper;
	@Autowired
	private ApiU_matchMapper apiU_matchMapper;

	// 新增对象后返回记录ID
	public int U_match_worksInsertion(ApiU_match_works u_match_works) {
		return this.apiU_match_worksMapper.U_match_worksInsertion(u_match_works);
	}

	@Override
	public List<ApiU_match_works> findU_match_worksByMatchId(Integer match_id) {

		return apiU_match_worksMapper.findU_match_worksByMatchId(match_id);
	}

	@Override
	public List<ApiU_match_works> findU_api_matchs_vote(Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		return apiU_match_worksMapper.findU_api_matchs_vote(params);
	}

	@Override
	public List<Map<String, Object>> findU_matchs_statistics_condition(Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		List<ApiU_match_works> list = apiU_match_worksMapper.findU_matchs_statistics_condition(params);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
		for (ApiU_match_works apiU_match_works : list) {
			map = new HashMap<String, Object>();
			map.put("vote_num", apiU_match_works.getVote_num());
			map.put("c_nick", apiU_match_works.getC_nick());
			map.put("c_avatar", apiU_match_works.getC_avatar());
			map.put("works_id", apiU_match_works.getWorks_id());
			listMap.add(map);
		}

		return listMap;
	}

	@Override
	public List<ApiU_match_works> findU_match_search_works(Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		return apiU_match_worksMapper.findU_match_search_works(params);
	}

	@Override
	public List<ApiU_match_works> findU_match_already_works(Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		return apiU_match_worksMapper.findU_match_already_works(params);
	}

	// 作品的详情；作品投票的信息；作品所属奖项的信息
	public Map<String, Object> findMatchworkInfo(Integer works_id) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		// 作品的信息
		ApiU_match_works apiU_match_works = apiU_match_worksMapper.findMatchworkInfo(works_id);
		// 作品所属奖项的信息
		if (apiU_match_works.getPrize_id() != 0) {
			ApiU_match_prize apiU_match_prize = apiU_match_prizeMapper
					.findMatchprizeInfo(apiU_match_works.getPrize_id());
			if (apiU_match_prize.getPrize_num() == 1) {// 是竞猜的奖项 序值为1表示是大奖
				ApiU_match apiU_match = apiU_matchMapper
						.findU_matchById(Integer.valueOf(apiU_match_works.getMatch_id()));
				// 竞猜奖金
				BigDecimal guess_amount = apiU_match.getGuess_amount();
				// 中了大奖的作品
				List<ApiU_match_works> list = apiU_match_prize.getApiU_match_works();
				Integer peoplenum = 0;// 总获奖人数
				for (ApiU_match_works match_works : list) {
					// 累加每个作品的投票人数
					peoplenum += Integer.valueOf(match_works.getVote_num());
				}
				BigDecimal money = guess_amount.divide(new BigDecimal(peoplenum), 2, RoundingMode.HALF_UP);
				result.put("guess_amount", money);
			}
		}
		result.put("match_works", apiU_match_works);
		return result;
	}

	@Override
	public List<Map<String, Object>> findU_match_statistics(Map<String, Object> params) throws Exception {
		List<Map<String, Object>> listResult = new ArrayList<Map<String, Object>>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Map<String, Object>> list = apiU_match_worksMapper.findU_match_statistics(params);
		for (Map<String, Object> map : list) {
			resultMap = new HashMap<String, Object>();
			resultMap.put("works_id", map.get("WORKS_ID"));
			resultMap.put("join_name", map.get("JOIN_NAME"));
			resultMap.put("works_name", map.get("WORKS_NAME"));
			resultMap.put("vedio_url", map.get("VEDIO_URL"));
			resultMap.put("match_id", map.get("MATCH_ID"));
			resultMap.put("user_id", map.get("USER_ID"));
			resultMap.put("c_nick", map.get("C_NICK"));
			resultMap.put("c_avatar", map.get("C_AVATAR"));
			resultMap.put("vote_num", map.get("VOTE_NUM"));
			resultMap.put("prize_id", map.get("PRIZE_ID"));
			listResult.add(resultMap);
		}
		return listResult;
	}

	public void insertMatchworks(ApiU_match_works u_match_works) throws Exception {
		Integer sort = apiU_match_worksMapper.getMatchMaxSort(Integer.valueOf(u_match_works.getMatch_id()));
		if (sort != null) {
			u_match_works.setSort(sort + 1);
		} else {
			u_match_works.setSort(1);
		}
		apiU_match_worksMapper.U_match_worksInsertion(u_match_works);
	}

}
